//
//  TZToolbar.m
//  BSBDJ
//
//  Created by user on 16/5/23.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZToolbar.h"
#import "TZPublishController.h"

@interface TZToolbar()

/** 发布按钮*/
@property (nonatomic ,weak)  UIButton *publishBtn;
/** 上一次点击的TabbarBtn*/
@property (nonatomic ,weak) UIControl *firsTabbarBtn;

@end

@implementation TZToolbar

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        UIButton *publishBtn = [[UIButton alloc] init];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:0];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [publishBtn sizeToFit];
        [publishBtn addTarget:self action:@selector(clickPublishBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:publishBtn];
        _publishBtn = publishBtn;
    }
    
    return self;
}

/** 拿不到导航控制器的情况下  用UIApplication的跟控制器跳转*/
- (void)clickPublishBtn{
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:[TZPublishController new] animated:YES completion:nil];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 原来的4个
    CGFloat width = self.width / 5;
    int index = 0;
    
    for (UIControl *control in self.subviews){
        if (![control isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        
        if (self.firsTabbarBtn == nil && index == 0) {
            self.firsTabbarBtn = control;
        }
        
        [control addTarget:self action:@selector(clickControl:) forControlEvents:UIControlEventTouchUpInside];
        // 计算UITabBarButton按钮位置
        control.width = width;
        control.x = index > 1? width *(index + 1) :width * index;
        

        index ++;
    }
    
    self.publishBtn.center = CGPointMake(self.width *0.5, self.height *0.5);
}

- (void)clickControl :(UIControl *)tabbarBtn{
    
    // 第一点击切换界面  第二次点击 刷新(第一个tabbarBtn 第一次只用点一次,就可以刷新因为  刚进来的时候就保存了  如果上一次和这次的tabbarBtn相同就刷新)
    if (tabbarBtn == self.firsTabbarBtn){
        [[NSNotificationCenter defaultCenter]postNotificationName:tabbarBtnClickNotificationCenter object:self userInfo:nil];
        NSLog(@"准备通知");
    }
    self.firsTabbarBtn = tabbarBtn;
}





@end
